package com.explam.springboot.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.explam.springboot.common.ResultUtil;
import com.explam.springboot.mapper.IncomeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.explam.springboot.service.IIncomeService;
import com.explam.springboot.entity.Income;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 收入表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-03-25
 */
@RestController
@RequestMapping("/admin/income")
public class IncomeController {

    @Resource
    private IIncomeService incomeService;

    @Autowired
    private IncomeMapper incomeMapper;

    /*
     * 获取总收入
     * */
    @GetMapping("/total-money")
    public ResultUtil getMoneySum(){
        Double total = incomeMapper.getMoneyTotal();
        return ResultUtil.success("家庭总收入",total);
    }

    /*
    * 获取一年内每个月的总收入
    * */
    @GetMapping("/month/total-monty")
    public ResultUtil getEveryMonthTotalMoney(){
        List<Map<String, Object>> mapList = incomeMapper.getEveryMonthTotalMoney();
        return ResultUtil.success(mapList);
    }

    //修改
    @PostMapping("/update")
    public ResultUtil update(@RequestBody Income income){
        incomeService.saveOrUpdate(income);
        return ResultUtil.success();
    }

    //新增
    @PostMapping("/save")
    public ResultUtil save(@RequestBody Income income){
        incomeService.save(income);
        return ResultUtil.success();
    }

        //删除
    @GetMapping("/delete/id={id}")
    public ResultUtil delete(@PathVariable Integer id) {
        incomeService.removeById(id);
        return ResultUtil.success();
    }

    //批量删除
    @PostMapping("/del/batch")
    public ResultUtil deleteBatch(@RequestBody List<Integer> ids){
         incomeService.removeByIds(ids);
        return ResultUtil.success();
    }

        //查询所有数据
    @GetMapping("/all")
    public ResultUtil findAll() {
        return ResultUtil.success(incomeService.list());
    }

        //根据id查询
    @GetMapping("/get-income/id={id}")
    public ResultUtil findOne(@PathVariable Integer id) {
        return ResultUtil.success(incomeService.getById(id));
    }

        //分页查询
    @GetMapping("/page")
    public ResultUtil findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String nickname,
                           @RequestParam(defaultValue = "") String title) {
        QueryWrapper<Income> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(nickname)){
            queryWrapper.like("nick_name", nickname);
        }
        if (!"".equals(title)){
            queryWrapper.like("title", title);
        }
        return ResultUtil.success(incomeService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }
}

